﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NanoERP.Business;

using LL.Utility.WindowsForms;
using NanoERP.Model.Poco;
using NanoERP.Infrastructure;
using NanoERP.Model.SystemMgmt;

namespace NanoERP.Modules.SystemMgmt
{
    //[Module("RoleMgr")]
    public partial class FrmSelectFuncsBase : EditFormBase
    {
        public FrmSelectFuncsBase()
        {
            InitializeComponent();
            InitControls();
        }

        protected void InitControls()
        {
            InitTreeView();
        }

        protected void InitTreeView()
        {

            var tvHelper = new TreeViewHelper(this.treeView1);
            tvHelper.EnableCheckAll();
        }

        

        protected void BuildNode(TreeNodeCollection nodes,
            IEnumerable<ModuleTree> roleTypes)
        {
            foreach (var type in roleTypes)
            {
                var node = new TreeNode(type.Name)
                {
                    Tag = type
                };
                BuildNode(node.Nodes, type.Children);
                BuildModules(node.Nodes, type);
                nodes.Add(node);
            }
        }

        protected static void BuildModules(TreeNodeCollection nodes,
            ModuleTree tree)
        {
            foreach (var role in tree.Modules)
            {
                var node = new TreeNode(role.Name)
                {
                    //Checked = role.Checked,
                    Tag = role,
                    ImageKey = "application_side_tree.png",
                    SelectedImageKey = "application_side_tree.png"
                };
                foreach (var right in role.Rights)
                {
                    node.Nodes.Add(new TreeNode(right.Name)
                    {
                        Checked = right.Checked,
                        Tag = right,
                        ImageKey = "key_go.png",
                        SelectedImageKey = "key_go.png"
                    });
                }
                nodes.Add(node);
            }
        }
       

        /// <summary>
        /// 递归获取所有选中角色Id
        /// </summary>
        protected void GetSelectValue(ICollection<Guid> list,
            TreeNodeCollection nodes)
        {
            foreach (TreeNode node in nodes)
            {
                var item = node.Tag as SYS_Right;
                if (item == null)
                {
                    GetSelectValue(list, node.Nodes);
                    continue;
                }
                if (node.Checked)
                    list.Add(item.Id);
            }
        }

    }
}
